package com.kakaxi.mybilibili.module.home.category.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.Xml;

import com.kakaxi.mybilibili.bean.CategoryHomeItemBean;
import com.kakaxi.mybilibili.utils.LogUtil;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 分区首页列表xml解析器
 * Created by kakaxi on 16/11/28.
 */

public class PullCategoryHomeParser {


    /**
     * 解析xml
     *
     * @param context 上下文
     * @param is      xml的输入流
     * @return 列表
     * @throws Exception 错误
     */
    public List<CategoryHomeItemBean> parse(Context context, InputStream is) throws Exception {
        List<CategoryHomeItemBean> categoryHomeItemBeanList = null;
        CategoryHomeItemBean categoryHomeItemBean = null;

        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(is, "UTF-8");

        int eventType = parser.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    categoryHomeItemBeanList = new ArrayList<>();
                    break;
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("item")) {
                        categoryHomeItemBean = new CategoryHomeItemBean();
                    } else if (parser.getName().equals("tid")) {
                        parser.next();
                        categoryHomeItemBean.setTid(Integer.parseInt(parser.getText()));
                    } else if (parser.getName().equals("searchname")) {
                        parser.next();
                        categoryHomeItemBean.setSearchname(parser.getText());
                    } else if (parser.getName().equals("captionname")) {
                        parser.next();
                        categoryHomeItemBean.setCaptionname(parser.getText());
                    } else if (parser.getName().equals("typename")) {
                        parser.next();
                        categoryHomeItemBean.setTypename(parser.getText());
                    } else if (parser.getName().equals("drawableId")) {
                        parser.next();
                        LogUtil.d(context.getPackageName());
                        int drawableId = context.getResources().getIdentifier(parser.getText(),
                                "drawable", context.getPackageName());
                        Drawable drawable = ContextCompat.getDrawable(context, drawableId);
                        categoryHomeItemBean.setCover(drawable);
                    } else if (parser.getName().equals("child")) {
                        categoryHomeItemBean.setChild(parseChildArray(parser));
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("item")) {
                        categoryHomeItemBeanList.add(categoryHomeItemBean);
                        categoryHomeItemBean = null;
                    }
                    break;
            }
            eventType = parser.next();
        }

        LogUtil.d(categoryHomeItemBeanList.toString());
        return categoryHomeItemBeanList;
    }

    /**
     * 解析子数组
     *
     * @param parser 解析器
     * @return 解析后的子数组
     * @throws Exception 错误
     */
    private List<CategoryHomeItemBean> parseChildArray(XmlPullParser parser) throws Exception {
        List<CategoryHomeItemBean> childList = new ArrayList<>();
        CategoryHomeItemBean child = null;
        int eventType = parser.getEventType();
        while (true) {
            switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (parser.getName().equals("item")) {
                        child = new CategoryHomeItemBean();
                    } else if (parser.getName().equals("tid")) {
                        parser.next();
                        child.setTid(Integer.parseInt(parser.getText()));
                    } else if (parser.getName().equals("typename")) {
                        parser.next();
                        child.setTypename(parser.getText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    if (parser.getName().equals("item")) {
                        childList.add(child);
                        child = null;
                    } else if (parser.getName().equals("child")) {
                        return childList;
                    }
                    break;
                case XmlPullParser.END_DOCUMENT: {
                    return childList;
                }
            }
            eventType = parser.next();
        }
    }

}
